package com.x.client.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.x.client.model.CarFavorite;
import com.x.client.rpc.CarFavoriteRpcService;
import com.x.erp.model.Car;
import com.x.erp.rpc.CarRpcService;
import com.x.framework.controller.BaseController;
import com.x.framework.controller.ResultCode;
import com.x.framework.controller.ResultModel;
import org.apache.commons.lang.StringUtils;
import org.springframework.validation.BindingResult;
import org.springframework.validation.ObjectError;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping(value = "/token/carFavorite")
public class CarFavoriteController extends BaseController{

	@Reference
	private CarFavoriteRpcService carFavoriteRpcService;// 车辆收藏服务

	@Reference
	private CarRpcService carRpcService;// 车辆收藏服务

	/**
	 * 新增车辆收藏信息
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/insertCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel insertCarFavorite(@Validated CarFavorite carFavorite, BindingResult bindingResult){
		if (bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		List<CarFavorite> list = carFavoriteRpcService.selectCarFavoriteList(carFavorite);
		if(list != null && list.size() > 0){
			return new ResultModel(ResultCode.RESULT_FAIL, "您已收藏该车辆！");
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "插入成功", carFavoriteRpcService.insertCarFavorite(carFavorite));
	}

	/**
	 * 查询用户车辆收藏信息
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/selectCarFavoriteList", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectCarFavoriteList(CarFavorite carFavorite){
		if (StringUtils.isEmpty(carFavorite.getUserId())){
			return new ResultModel(ResultCode.RESULT_FAIL, "用户ID不能为空！");
		}
		Car car = new Car();
		List<Car> carList = new ArrayList<>();
		List<CarFavorite> list = carFavoriteRpcService.selectCarFavoriteList(carFavorite);
		for (CarFavorite carFavoriteBack : list){
			car.setCarId(carFavoriteBack.getCarId());
			Car carBack = carRpcService.selectCar(car);
			// 车辆信息不为空，就加到集合中
			if(carBack!=null){
				carList.add(carBack);
			}else{
				// 车辆信息为空，就删除该车辆Id的所有收藏信息
				CarFavorite favoriteTemp = new CarFavorite ();
				favoriteTemp.setCarId (carFavoriteBack.getCarId());
				carFavoriteRpcService.deleteCarFavorite (favoriteTemp);
			}
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", carList);
	}

	/**
	 * 查询单个车辆收藏状态
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/selectCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel selectCarFavorite(@Validated CarFavorite carFavorite, BindingResult bindingResult) {
		if (bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		List<CarFavorite> list = carFavoriteRpcService.selectCarFavoriteList(carFavorite);
		Map<String, Object> map = new HashMap<>();
		if(list == null || list.size() == 0){
			map.put("favoriteState", 0);
		}else{
			map.put("favoriteState", 1);
			map.put("carFavoriteId", list.get(0).getCarFavoriteId());
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "查询成功", map);
	}

	/**
	 * 取消收藏
	 * @param carFavorite
	 * @return
	 */
	@RequestMapping(value = "/deleteCarFavorite", method = { RequestMethod.POST, RequestMethod.GET })
	public ResultModel deleteCarFavorite(@Validated CarFavorite carFavorite, BindingResult bindingResult){
		if (bindingResult.hasErrors()){
			List<ObjectError> errors = bindingResult.getAllErrors();
			return new ResultModel(ResultCode.RESULT_FAIL, errors.get(0).getDefaultMessage());
		}
		return new ResultModel(ResultCode.RESULT_SUCCESS, "删除成功", carFavoriteRpcService.deleteCarFavorite(carFavorite));
	}
}
